//
//  SWWelcomeController.swift
//  SinaWeibo
//
//  Created by wszyxc on 16/7/22.
//  Copyright © 2016年 wszyxc. All rights reserved.
//

import UIKit

import SDWebImage

class SWWelcomeController: UIViewController {
    
    override func loadView() {
        
        
        view = bgImageView
    
        
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        UIView.animateWithDuration(2, delay: 0.5, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.2, options: [], animations: { () -> Void in
            
            self.iconImageView.snp_updateConstraints(closure: { (make) -> Void in
                
                make.bottom.equalTo(self.view).offset(-400)
            })
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    
                    self.iconLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        
                        //动画执行完,发送通知告知 需要跳转控制器
                        
                        NSNotificationCenter.defaultCenter().postNotificationName(WelcomeNoti, object: "welcome")
                        
                })
                

                
        }
    }

  private func setupUI(){
        
        view.addSubview(iconImageView)
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view.snp_centerX)
            
            make.bottom.equalTo(view).offset(-200)
            
            make.width.height.equalTo(120)
            
        }
        
        view.addSubview(iconLabel)
        
        iconLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view)
            
            make.top.equalTo(iconImageView.snp_bottom).offset(16)
            
        }
        
        
    }
    
    
    //懒加载背景图片
    
  private  lazy var bgImageView:UIImageView = {
        
       let imageView = UIImageView(image: UIImage(named: "ad_background"))
        
    
        
        return imageView
    }()
    
    //懒加载头像
  private  lazy var iconImageView:UIImageView = {
        
        let imageView = UIImageView()
        
        imageView.contentMode = .ScaleAspectFill
        
        let imgStr = SWUserAcountViewModel.sharedUserAcountViewModel.userAcountModel?.avatar_large
        
        print(imgStr)
        
        let url = NSURL(string: imgStr ?? "")
        
        imageView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        
        imageView.layer.borderColor = THEMECOLOR.CGColor
        
        imageView.layer.borderWidth = 5
        
        imageView.cornerRadiu = 60
        
        imageView.layer.masksToBounds = true
        
        print(imageView.frame)
        
        return imageView
        
    }()
    
    //懒加载label
    
  private  lazy var iconLabel:UILabel = {
        
       let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 18)
        
        label.text = "欢迎回来"
        
        label.textAlignment = .Center
        
        label.alpha = 0
        
        return label
        
    }()
    
}
